package com.ruoyi.test.web;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.test.aspect.ManImpl;
import com.ruoyi.test.aspect.WomanImpl;
import com.ruoyi.test.domain.Message;
import com.ruoyi.test.domain.Person;
import com.ruoyi.test.serv.IPersonServ;
import com.ruoyi.test.util.WebSocketServer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Hello，示例spring boot层次结构编程操作示例
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Resource
    private IPersonServ personServ;
    /**
     * 访问首页，提示语
     */
    @GetMapping("/")
    public String index() {
        System.out.println(new Throwable().getStackTrace()[0]);
        return "这是控制层返回的信息，调用了index()方法，全路径为【/hello/】";
    }

    @GetMapping("/listAllPersonOrderByUsername")
    public JSONObject listAllPersonOrderByUsername() {
        return personServ.listAllPersonOrderByUsername();
    }

    @GetMapping("/online-users")
    public List<WebSocketServer.WebSocketSessionInfo> getOnlineUsers() {
        return WebSocketServer.listOnlineUsers();
    }


    //http://localhost:8080/hello/findPersonById/007
    @GetMapping("/getOnePerson/{id}")
    public Person getPerson(@PathVariable String id){
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        return personServ.getOne(id);
    }
    //http://localhost:80880/hello/findPersonById?id=007
    @GetMapping("/findPersonById")
    public Person findPersonById(String id){
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        return personServ.getOne(id);
    }
    @GetMapping("/findPerson")
    public Person findPerson(Person vo){
        System.out.println(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        return personServ.findPerson(vo);
    }
    @GetMapping("/pub/Get1")
    public Object pubGet1(){
        System.out.println(new Throwable().getStackTrace()[0]);
        return "/pub/Get1";
    }
    @GetMapping("/pub/Get2")
    public Object pubGet2(){
        System.out.println(new Throwable().getStackTrace()[0]);
        return "/pub/Get2";
    }

    @GetMapping("/pub/getOnline")
    public Object getOnline(){
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("onlineCidSet", WebSocketServer.getOnlineCidSet());
        o.put("onlineCount", WebSocketServer.getOnlineCount());
        o.put("onlineSessionIdSet", WebSocketServer.getOnlineSessionIdSet());
        return o;
    }

    @Resource private ManImpl man;
    @GetMapping("/pub/testAOP4Man")
    public Object testAOP4Man(){
        System.out.println(new Throwable().getStackTrace()[0]);
        man.takeAWash();
        return "/pub/testAOP4Man";
    }
    @Resource private WomanImpl woman;
    @GetMapping("/pub/testAOP4Woman")
    public Object testAOP4Woman(){
        System.out.println(new Throwable().getStackTrace()[0]);
        woman.takeAWash();
        return "/pub/testAOP4Woman";
    }

    @GetMapping("/listAllPerson")
    public Object listAllPerson(){
        System.out.println(new Throwable().getStackTrace()[0]);
        return personServ.listAllNg();
    }

    @GetMapping("/getCurrentPerson")
    public Object getCurrentPerson(HttpSession session){
        System.out.println(new Throwable().getStackTrace()[0]);
        return (Person)session.getAttribute("currentPerson");
    }

    @PutMapping("/updatePerson")
    public Object updatePerson(@RequestBody Person vo) {
        System.out.println(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        return personServ.update(vo);
    }

    //hello/loginPerson
    @PostMapping("/loginPerson")
    public JSONObject loginPerson(@RequestBody Person vo, HttpSession session) {
        System.out.println(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        JSONObject o = personServ.login(vo);
        if(o.getBoolean("success")){
            session.setAttribute("currentPerson", o.getObject("vo", Person.class));
        }
        return o;
    }
    @PostMapping("/logutPerson")
    public void logutPerson(HttpSession session) {
        System.out.println(new Throwable().getStackTrace()[0]);
        session.removeAttribute("currentPerson");
    }
    @PostMapping("/addPerson")
    public Object addPerson(@RequestBody Person vo) {
        System.out.println(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        return personServ.add(vo);
    }

    @DeleteMapping("/deletePerson")
    public Object deletePerson(String id) {
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        return personServ.delete(id);
    }

}
